/*
# ------------------------------------------------------------------------
# JD Base Theme
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
 */
/*Remove active class of Parent Menus */
jQuery(function() {
	var act = jQuery('.jd-admin-menu').find('.active').size();
	jQuery('.jd-admin-menu .active').each(function(i) {
		if (i != act - 1) {
			jQuery(this).removeClass('active');
		}
	});
});
jQuery(document).ready(function() {
	var pos = 0;
	var currentKey = -1;
	var currentUrl = null;
	var caption = jQuery('.jd-admin-menu-caption');
	var search = jQuery('#jd-admin-menu-search');
	var main = jQuery('.jd-admin-menu-main');
	var content = jQuery('.jd-admin-menu-content');
	var ul = jQuery('.jd-admin-menu');
	var cpanel = jQuery('#ja-cpanel-toggle');
	var height = main.height();
	main.css('position', 'absolute');
	main.css('top', -height - 10 + 'px');
	search.val('');
	jQuery.expr[':'].Contains = function(a, i, m) {
		return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0;
	};
	/* Focus an item when use arrow keys to select */
	function selectitem(i) {
		var item = jQuery('.show', ul).eq(i);
		jQuery('a', item).focus();
		currentUrl = jQuery('a', item).attr('href');
	}
	/* keyup event handle for search box */
	function keyUp(code) {
		if (code == 40) { // Arrow Down
			if (currentKey == -1) {
				currentKey++;
				selectitem(currentKey);
			}
			return false;
		}
		if (code == 27) { // Escape keycode

			search.val('');
		}
		currentKey = -1;
		search.change();
		return false;
	}
	/* keydown event handle for content box */
	content.keydown(function(e) {
		var code = (e.keyCode ? e.keyCode : e.which);
		if (code == 40) { // Arrow Down
				var count = ul.find('.show').size();
				if (currentKey == count - 1)
					return false;
				currentKey++;
				selectitem(currentKey);
				return false;
			}
			if (code == 38) {// Arrow Up when reach top item
				if (currentKey == -1 || currentKey == 0) {
					search.focus();
					return false;
				}
				currentKey--;
				selectitem(currentKey);
				return false;
			}
			if (code == 13) { // Enter
				window.location = currentUrl;
			}
			if (code == 27) {// Escape
				search.focus();
			}
			return false;
		});
	/* Remove item focus when mouse over on items */
	jQuery('li', ul).each(function() {
		jQuery(this).mouseover(function() {
			jQuery('.show a', ul).eq(currentKey).blur();
			currentKey = -1;
		});
	});
	caption.click(function() {
		if (jQuery('a', caption).hasClass('close')) {
			if (cpanel.hasClass('open')) {
				cpanel.click();
			}
			main.show();
			main.stop(true, false).animate( {
				top : 0
			}, 500);
			currentKey = -1;
			jQuery('a', caption).removeClass('close');
			jQuery('a', caption).addClass('open');
			// Scroll to active item
			if (!pos && jQuery('.active', ul).offset() != undefined)
				pos = jQuery('.active', ul).offset().top;
			if (pos == 0)
				pos = -200;
			content.animate( {
				scrollTop : pos + 200
			});
			search.focus();
		} else {
			// main.slideUp();
			main.stop(true, false).animate( {
				top : -height - 10 + 'px'
			}, 700, function() {
				// main.css('opacity', 1);
				});
			jQuery('a', caption).removeClass('open');
			jQuery('a', caption).addClass('close');
		}
	});
	search.change(function() {
		var val = jQuery(this).val();
		main.removeHighlight();
		if (val) {
			var match = ul.find("a:Contains(" + val + ")");
			var nomatch = ul.find("a:not(:Contains(" + val + "))");
			nomatch.parent().hide().removeClass('show').addClass('hide');
			match.parent().show().removeClass('hide').addClass('show');
			match.highlight(val);
			match = undefined;
			nomatch = undefined;
			val = undefined;
		} else {
			ul.find('li').show().removeClass('hide').addClass('show');
		}
	}).keyup(function(e) {
		var code = (e.keyCode ? e.keyCode : e.which);
		keyUp(code);
		code = undefined;
	});
	jQuery(document).keydown(function(e) {
		var code = (e.keyCode ? e.keyCode : e.which);
		if (e.shiftKey == true && e.ctrlKey == true && code == 83) {
			// Ctrl + Shift + S
			caption.click();
			return false;
		}
	});
});